package com.xd.lib.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.xd.lib.R;
import com.xd.lib.tool.util.SoundUtil;

/**
 * Created by Administrator on 2015/6/15.
 */
public class DiscActivity extends Activity {

    /**
     * 盘片旋转动画
     */
    private Animation discAnim;
    /**
     * 拨杆下拉动画
     */
    private Animation handlerPullAnim;
    /**
     * 拨杆恢复动画
     */
    private Animation handlerPushAnim;
    /**
     * 盘片
     */
    private ImageView imageDisc;
    /**
     * 拨杆
     */
    private ImageView imageHandler;
    /**
     * 播放按钮
     */
    private ImageButton btnPlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disc_layout);

        /**
         * 初始化盘片动画，通过LinearInterpolator使用线性速度（匀速）
         */
        discAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);
        LinearInterpolator discInterpolator = new LinearInterpolator();
        discAnim.setInterpolator(discInterpolator);
        discAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                imageHandler.startAnimation(handlerPushAnim);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        /**
         * 初始化拨杆下拉的动画
         */
        handlerPullAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_45);
        LinearInterpolator handlerPullInterpolator = new LinearInterpolator();
        handlerPullAnim.setInterpolator(handlerPullInterpolator);
        handlerPullAnim.setFillAfter(true);//动画结束后保持当前状态
        handlerPullAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                imageDisc.startAnimation(discAnim);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        /**
         * 初始化拨杆弹回的动画
         */
        handlerPushAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_d_45);
        LinearInterpolator handlerPushInterpolator = new LinearInterpolator();
        handlerPushAnim.setInterpolator(handlerPushInterpolator);
        handlerPushAnim.setFillAfter(true);
        handlerPushAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                btnPlay.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        /**
         * 开始播放
         * 1、拨杆旋转到盘片上
         * 2、盘片开始旋转
         * 3、盘片旋转完，拨杆返回
         */
        imageDisc = (ImageView)findViewById(R.id.image_pan_disc);
        imageHandler = (ImageView)findViewById(R.id.image_pan_handler);
        btnPlay = (ImageButton)findViewById(R.id.btn_play_start);
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始拨杆进入动画
                btnPlay.setVisibility(View.GONE);
                imageHandler.startAnimation(handlerPullAnim);

                //播放音乐
                SoundUtil.playSong(DiscActivity.this, "qi_li_xiang.m4a");
            }
        });
    }

    @Override
    protected void onPause() {
        /**
         * 清空游戏状态
         */
        imageDisc.clearAnimation();
        SoundUtil.stopSong(DiscActivity.this);

        super.onPause();
    }
}
